package algorithm.翻转链表;

public class Solution {

    public static void main(String[] args) {
        //申明一个链表
        ListNode head = new ListNode(1);
        //指针指向链表头
        ListNode pointer = head;
        for (int i = 0; i < 3; i++) {
            pointer.next = new ListNode(pointer.value + 1);
            pointer = pointer.next;
        }
        //原始--打印
        System.out.println("原始：");
        print(head);


        //翻转后--打印
        System.out.println("翻转后：");
        print(reverse(head));
    }

    private static ListNode reverse(ListNode head) {
        //pre指针
        ListNode pre = null;
        //curr指针
        ListNode curr = head;
        while (curr != null) {
            //临时变量next
            ListNode next = curr.next;
            curr.next = pre;
            pre = curr;
            curr = next;
        }
        return pre;
    }


    private static void print(ListNode node) {
        if (node == null) {
            return;
        }
        System.out.println(node.value);
        print(node.next);
    }

}

class ListNode {
    int value;
    ListNode next;

    public ListNode(int value) {
        this.value = value;
    }

}


// 作者：reals
// 链接：https://leetcode.cn/problems/reverse-nodes-in-k-group/solution/tu-jie-kge-yi-zu-fan-zhuan-lian-biao-by-user7208t/
// 来源：力扣（LeetCode）
// 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
